<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');

$veepariBookCr = array();
$veepariBookDr = array();
$tempArr       = array();
$tempArr2      = array();
$party         = array();
$total         = array();
$opening       = array();
$partyId       = 0;
$fromDate      = 0;
$toDate        = 0;
if(isset($_POST['party']) && $_POST['party'] > 0)
{
  $partyId   = isset($_POST['party']) ? $_POST['party'] : 0;
  $fromDate = isset($_REQUEST['fromDateYear']) ? $_REQUEST['fromDateYear']."-".$_REQUEST['fromDateMonth']."-".$_REQUEST['fromDateDay'] : 0;
  $toDate  = isset($_POST['toDateYear']) ? $_POST['toDateYear']."-".$_POST['toDateMonth']."-".$_POST['toDateDay'] : 0;
  
  $selectVeepariBook = "SELECT vepaaribillmaster.vepaariBookMasterId,vepaaribillmaster.partyId,vepaaribillmaster.vepaariBookDate,
                               vepaaribilldetail.vepaariBookDetailId,vepaaribilldetail.particulars,vepaaribilldetail.weight,vepaaribilldetail.touch,
                               vepaaribilldetail.fine,vepaaribilldetail.amount,
                               vepaaribilldetail.CrDr
                          FROM vepaaribillmaster
                          JOIN vepaaribilldetail
                         WHERE vepaaribillmaster.partyId = ".$partyId."
                           AND DATE_FORMAT(vepaariBookDate,'%Y-%m-%d') <= '".$toDate."'
                         ORDER BY vepaaribillmaster.vepaariBookDate,vepaaribilldetail.vepaariBookDetailId";
  $selectVeepariBookRes = mysql_query($selectVeepariBook);


  $crCount = 0;
  $drCount = 0;
  $total['weight']   = 0;
  $total['crFine']   = 0;
  $total['crAmount'] = 0;
  $total['drFine']   = 0;
  $total['drAmount'] = 0;
  
  $opening['crAmount']      = 0;
  $opening['drAmount']      = 0;
  $opening['crWeight']      = 0;
  $opening['crTouch']       = 0;
  $opening['crFine']        = 0;
  $opening['drFine']        = 0;
  while($row = mysql_fetch_assoc($selectVeepariBookRes))
  {
     
    if($row['vepaariBookDate'] >= $fromDate)
    {

      if($row['CrDr'] == "Cr")
      {
        $veepariBookCr[$crCount]['vepaariBookMasterId	']   = $row['vepaariBookMasterId'];
        $veepariBookCr[$crCount]['vepaariBookDetailId	']   = $row['vepaariBookDetailId'];
        $veepariBookCr[$crCount]['vepaariBookDate']        = $row['vepaariBookDate'];
        $veepariBookCr[$crCount]['particulars']            = $row['particulars'];
        $veepariBookCr[$crCount]['weight']                 = $row['weight'];
        $veepariBookCr[$crCount]['touch']                  = $row['touch'];
        $veepariBookCr[$crCount]['fine']                   = $row['fine'];
        $veepariBookCr[$crCount]['amount']                 = $row['amount'];
        $crCount++;
  
      }
      else
      {
        $veepariBookDr[$drCount]['vepaariBookMasterId	']   = $row['vepaariBookMasterId'];
        $veepariBookDr[$drCount]['vepaariBookDetailId	']   = $row['vepaariBookDetailId'];
        $veepariBookDr[$drCount]['vepaariBookDate']        = $row['vepaariBookDate'];
        $veepariBookDr[$drCount]['particulars']            = $row['particulars'];
        $veepariBookDr[$drCount]['weight']                 = $row['weight'];
        $veepariBookDr[$drCount]['touch']                  = $row['touch'];
        $veepariBookDr[$drCount]['fine']                   = $row['fine'];
        $veepariBookDr[$drCount]['amount']                 = $row['amount'];
        $drCount++;
      }
    }
 
    else
    {//this get opening balance : Start
      if($row['CrDr'] == "Cr")
      {
        $opening['crAmount'] += $row['amount'];
        $opening['crWeight'] += $row['weight'];
        $opening['crTouch']  += $row['touch'];
        $opening['crFine']   += $row['fine'];
      }
      else
      {
        $opening['drFine']   += $row['fine'];
        $opening['drAmount'] += $row['amount'];
      }
    }//this get opening balance : End
    
    if($row['CrDr'] == "Cr")
    {
      $total['weight']   += $row['weight'];
      $total['crFine']   += $row['fine'];
      $total['crAmount'] += $row['amount'];
    }
    else
    {
      $total['drFine']   += $row['fine'];
      $total['drAmount'] += $row['amount'];
    }
  }

  $billBook = "SELECT billBookId,billNo,DATE_FORMAT(billBookDate,'%Y-%m-%d') AS billBookDate,totalFine,totalAmount FROM billbook
                WHERE partyId = ".$partyId."
                  AND DATE_FORMAT(billBookDate,'%Y-%m-%d') <= '".$toDate."'
                ORDER BY billBookId";
  $billBookRes = mysql_query($billBook);
  while($billBook = mysql_fetch_assoc($billBookRes))
  {
    if($billBook['billBookDate'] >= $fromDate)
    {
      $veepariBookDr[$drCount]['vepaaribookId']   = "";
      $veepariBookDr[$drCount]['vepaariBookDate'] = $billBook['billBookDate'];
      $veepariBookDr[$drCount]['particulars']     = "Bill No. ".$billBook['billNo'];
      $veepariBookDr[$drCount]['fine']            = $billBook['totalFine'];
      $veepariBookDr[$drCount]['amount']          = $billBook['totalAmount'];
      $veepariBookDr[$drCount]['deleteLink']      = "";
      $drCount++;
    }
    else
    { //this get opening balance
      $opening['drFine']   += $billBook['totalFine'];  
      $opening['drAmount'] += $billBook['totalAmount'];  
    }
    $total['drFine']   += $billBook['totalFine'];
    $total['drAmount'] += $billBook['totalAmount'];
  }
  
  $total['netFine']   = ($total['drFine'] - $total['crFine'] );
  $total['netAmount'] = ($total['drAmount'] - $total['crAmount']);
}

// This for sort debit array : Start
foreach ($veepariBookDr as $key => $value) 
{
  $tempArr[$key] = $value['vepaariBookDate'];
}
array_multisort($tempArr,SORT_ASC,SORT_STRING,$veepariBookDr);
// This for sort debit array : End

// This for sort credit array : Start
foreach ($veepariBookCr as $key => $value) 
{
  $tempArr2[$key] = $value['vepaariBookDate'];
}
array_multisort($tempArr2,SORT_ASC,SORT_STRING,$veepariBookCr);
// This for sort credit array : End

$selectParty = "SELECT partyId,partyName,partyFromDate
                  FROM party
                 ORDER BY partyName";
$selectPartyRes = mysql_query($selectParty);
$a = 0;

while($partyRow = mysql_fetch_assoc($selectPartyRes))
{
  $party['partyId'][$a]       = $partyRow['partyId'];
  $party['partyName'][$a]     = $partyRow['partyName'];
  $party['partyFromDate'][$a] = $partyRow['partyFromDate'];
  $a++;
   if(isset($_POST['bdate']) == "1" && $partyRow['partyId'] == $_POST['party'])
  {
    $fromDate = $partyRow['partyFromDate'];
  }

 
}
$smarty = new Smarty;
require("bottom.php");
$smarty->assign("l_partySelect",$l_partySelect);
$smarty->assign("l_from",$l_from);
$smarty->assign("l_to",$l_to);
$smarty->assign("l_date",$l_date);
$smarty->assign("l_particulars",$l_particulars);
$smarty->assign("l_weight",$l_weight);
$smarty->assign("l_touch",$l_touch);
$smarty->assign("l_fine",$l_fine);
$smarty->assign("l_amount",$l_amount);
$smarty->assign("l_credit",$l_credit);
$smarty->assign("l_debit",$l_debit);
$smarty->assign("l_total",$l_total);
$smarty->assign("l_find",$l_find);
$smarty->assign("l_netFine",$l_netFine);
$smarty->assign("l_netAmount",$l_netAmount);
$smarty->assign("l_delete",$l_delete);
$smarty->assign("l_edit",$l_edit);

$smarty->assign("veepariBookCr",$veepariBookCr);
$smarty->assign("veepariBookDr",$veepariBookDr);
$smarty->assign("party",$party);
$smarty->assign("total",$total);
$smarty->assign("partyId",$partyId);
$smarty->assign("fromDate",$fromDate);
$smarty->assign("toDate",$toDate);
$smarty->assign("opening",$opening);

$smarty->display("vepaariBookDetail.tpl");
?>